package com.huitian.mobileshop.activity;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Set;

import android.app.Activity;
import android.app.ActivityManager;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.DialogInterface.OnClickListener;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.JavascriptInterface;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.huitian.mobileshop.Links;
import com.huitian.mobileshop.ProductContainer;
import com.huitian.mobileshop.R;
import com.huitian.mobileshop.Utils;

public class MainActivity extends Activity {
	
	public final static String TITLE="title";
	public final static String URL="url";
	public final static String USERBUTTON="userbutton";
	public final static String BACKBUTTON="backbutton";
	public final static String RELOAD="reload";
	public final static String BACK="back";
	private WebView webView;
	private TextView titletext;
	private Button userbutton,backbutton;
	private Handler mHandler=new Handler();
	private String url="";
	//生命周期函数onCreate
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		webView=(WebView)findViewById(R.id.web);
		titletext=(TextView)findViewById(R.id.title_text);
		userbutton=(Button)findViewById(R.id.button_user);
		backbutton=(Button)findViewById(R.id.button_back);
		Intent intent=getIntent();
		titletext.setText(intent.getStringExtra("title"));
		userbutton.setVisibility(intent.getIntExtra("userbutton", View.VISIBLE));
		backbutton.setVisibility(intent.getIntExtra("backbutton", View.VISIBLE));
		url=intent.getStringExtra("url");
		Utils.initWebViewSettings(webView,MainActivity.this);
		webView.addJavascriptInterface(new Js(), "androidjs");
		Utils.initWeb(url, this, webView);
	}
	@Override
	protected void onResume() {
		super.onResume();
		System.out.println("abdddc");
		if(Links.MAIN.equals(url)&&ProductContainer.isEmpty()){
			webView.reload();
		}
	}
	//用户中心按钮的功能，用于打开用户个人中心
	public void showUserCenter(View view){
		startActivity(new Intent(MainActivity.this,PersonalActivity.class));
	}
	
	//goback按钮执行动作，返回到上一个activity界面
	public void goback(View view ){
		this.finish();
	}
	
	//网页初始化函数
	public void initWeb(String url){
		if(!Utils.isNetworkEnable(this)){
			Toast.makeText(this, "当前无网络连接", Toast.LENGTH_SHORT).show();
			url="file:///android_asset/networkfailed.html";
		}
		webView.loadUrl(url);
	}
	
	/*public boolean onKeyDown(int keyCode, KeyEvent event) {
		if(keyCode==KeyEvent.KEYCODE_BACK){
			if(webView.canGoBack()) webView.goBack();
			else return super.onKeyDown(keyCode, event);
		}
		return true;
	}*/
	
	//js方法库，用于网页调用本地android功能
	class Js{
		@JavascriptInterface
		public void openactivity(){
			mHandler.post(new Runnable() {
				@Override
				public void run() {
					Intent intent=new Intent(MainActivity.this, TestActivity.class);
					startActivity(intent);
				}
			});
		}
		@JavascriptInterface
		public void topersonalcenter(){
			startActivity(new Intent(MainActivity.this,PersonalActivity.class));
			MainActivity.this.finish();
		}
		
		public String getCustomerCode(){
			return "";
		}
		@JavascriptInterface
		public void clear(){
			ProductContainer.clear();
		}
		
		@JavascriptInterface
		public void tomain() {
			Intent intent=new Intent(MainActivity.this, MainActivity.class);
			intent.putExtra(MainActivity.TITLE, "商品大全").putExtra(MainActivity.URL, Links.MAIN).putExtra(MainActivity.USERBUTTON, View.VISIBLE);
			startActivity(intent);
			MainActivity.this.finish();
		}
		//当网络访问失败时，提示界面中“重试”要执行的函数
		@JavascriptInterface
		public void retry(){
			mHandler.post(new Runnable() {
				@Override
				public void run() {
					Utils.initWeb(url,MainActivity.this,webView);
				}
			});
		}
		//显示网页访问的进度提示
		@JavascriptInterface
		public void showprogress(String ss )
		{
			final String s =ss;
			mHandler.post(new Runnable() {
				@Override
				public void run() {
					ProgressDialog.show(MainActivity.this,null,s,true);
				}
			});
		}
		//本地购物车的修改，plus负责商品的添加，sub负责商品的移除
		
		
		@JavascriptInterface
		public String plus(String productcode, float price){
			return ProductContainer.plus(productcode, price);
		}
		
		@JavascriptInterface
		public String sub(String productcode,float price){
			return ProductContainer.sub(productcode, price);
		}
		
		@JavascriptInterface
		public void tocreateorder(){
			final HashMap<String, Integer> products=ProductContainer.products();
			StringBuffer params=new StringBuffer("?abc=abc");
			Set<String> keys=products.keySet();
			for(String key :keys){
				params.append("&"+key+"="+products.get(key));
			}
			String link=Links.CREATEORDER+params;
			Intent intent=new Intent(MainActivity.this,MainActivity.class);
			intent.putExtra(MainActivity.URL, link).putExtra(MainActivity.TITLE, "创建订单").putExtra(MainActivity.USERBUTTON, View.VISIBLE);
			startActivity(intent);
		}
		
		@JavascriptInterface
		public void show_address(String code){
			String link=Links.SHOW_ADDRESS_DETAIL+code;
			Intent intent=new Intent(MainActivity.this,MainActivity.class);
			intent.putExtra(MainActivity.URL, link).putExtra(MainActivity.TITLE, "地址详情").putExtra(MainActivity.USERBUTTON, View.GONE);
			startActivity(intent);
		}
		
		@JavascriptInterface
		public void show_order(String code){
			String link=Links.SHOW_ORDER_DETAIL+code;
			Intent intent=new Intent(MainActivity.this,MainActivity.class);
			intent.putExtra(MainActivity.URL, link).putExtra(MainActivity.TITLE, "订单详情").putExtra(MainActivity.USERBUTTON, View.GONE);
			startActivity(intent);
		}
		
		@JavascriptInterface
		public void show_toast(String msg){
			Toast.makeText(MainActivity.this, msg, Toast.LENGTH_SHORT).show();
		}
		boolean res =false;
		@JavascriptInterface
		public boolean show_alertDialog(String msg){
			
			AlertDialog.Builder b =new AlertDialog.Builder(MainActivity.this);
			final AlertDialog d =b.setTitle("确认").setMessage(msg).setPositiveButton("是", new OnClickListener() {
				
				@Override
				public void onClick(DialogInterface arg0, int arg1) {
					// TODO Auto-generated method stub
					res=true;
				}
			}).setNegativeButton("否", new OnClickListener() {
				
				@Override
				public void onClick(DialogInterface arg0, int arg1) {
					// TODO Auto-generated method stub
					res=false;
				}
			}).show();
			return res;
			
		}
	}//end Js
	
} //end MainActivity
